package com.demo.studentmanager;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.demo.studentmanager.databinding.ActivityEditCourseBinding;

public class EditCourseActivity extends AppCompatActivity {
    private Course course;
    private DBHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityEditCourseBinding binding = ActivityEditCourseBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        course = (Course) getIntent().getSerializableExtra("course");
        dbHelper = new DBHelper(this);
        if (course != null) {
            binding.etName.setText(course.getName());
            binding.etTeacher.setText(course.getTeacher());
            binding.btnDelete.setVisibility(View.VISIBLE);
        } else {
            binding.btnDelete.setVisibility(View.GONE);
        }


        binding.btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = binding.etName.getText().toString();
                String teacher = binding.etTeacher.getText().toString();
                if (name.isEmpty() || teacher.isEmpty()) {
                    Toast.makeText(EditCourseActivity.this, "请完成所有内容！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (course!=null){
                    course .setName(name);
                    course.setTeacher(teacher);
                    dbHelper.updateCourse(course);
                }else {
                    course = new Course();
                    course .setName(name);
                    course.setTeacher(teacher);
                    dbHelper.addCourse(course);
                }
                Toast.makeText(EditCourseActivity.this, "保存成功！", Toast.LENGTH_SHORT).show();
                finish();


            }
        });
        binding.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(EditCourseActivity.this)
                        .setTitle("注意")
                        .setMessage("确定要删除此课程吗？")
                        .setPositiveButton("取消", null)
                        .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dbHelper.deleteCourse(course);
                                Toast.makeText(EditCourseActivity.this, "删除成功！", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        }).show();
            }
        });
    }

    public void back(View view) {
        finish();
    }
}